//
//  SettingsManager.m
//  SettingsPage
//
//  Created by CPX on 2018/6/15.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "SettingsManager.h"
//#import "QuranSetting.h"

#define ASR @"setting_asr"
#define HighLatitude @"setting_highLatitude"
#define KeyPrayTimeCalMeth @"praytimecalmeth"
//#define ReadingProgress @"setting_readingprogress"


/**
 设置页相关设置类
 */
@interface SettingsManager()

@property (nonatomic,strong) NSUserDefaults *defaults;
@property (nonatomic,copy) NSString *path;

@end

@implementation SettingsManager

+ (instancetype)sharedManager{
    static SettingsManager *setting = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        setting = [self new];
        [setting _init];
    });
    return setting;
}

- (void)_init{
    self.defaults = [NSUserDefaults standardUserDefaults];
    id asr = [self.defaults objectForKey:ASR];
    self.asr = asr?[asr integerValue]:0;
    
    id highL = [self.defaults objectForKey:HighLatitude];
    self.highLatitude = highL?[highL integerValue]:1;
    
    id prayTimeC = [self.defaults objectForKey:KeyPrayTimeCalMeth];
    self.prayTimeCalMeth = prayTimeC?[prayTimeC integerValue]:3;
    
//    id prog = [self.defaults objectForKey:ReadingProgress];
//    self.enableReadProgress = prog?[prog boolValue]:YES;
    
    [AppDataManager sharedManager].asr = self.asr;
    [AppDataManager sharedManager].highLatitude = self.highLatitude;
    [AppDataManager sharedManager].prayTimeCalMeth = self.prayTimeCalMeth;
//    [AppDataManager sharedManager].enableReadProgress = self.enableReadProgress;
    
    NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    self.path = [path stringByAppendingPathComponent:@"settings"];
    self.tranLan = [NSKeyedUnarchiver unarchiveObjectWithFile:[NSString stringWithFormat:@"%@/tranLan",self.path]];
    if (self.tranLan == nil) {
        self.tranLan = [LanguageModel new];
        self.tranLan.display_name = @"English";
        self.tranLan.author = @"Transliteration";
    }
    self.mp3Lan = [NSKeyedUnarchiver unarchiveObjectWithFile:[NSString stringWithFormat:@"%@/mp3Lan",self.path]];
    if (self.mp3Lan == nil) {
        self.mp3Lan = [LanguageModel new];
        self.mp3Lan.display_name = @"Al-Ghamdi";
        self.mp3Lan.author = @"ghamdi";
    }
}

- (void)saveAsr:(NSInteger)asr{
    self.asr = asr;
    [self.defaults setInteger:self.asr forKey:ASR];
    [AppDataManager sharedManager].asr = self.asr;
}

- (void)saveHighLatitude:(NSInteger)highLatitude{
    self.highLatitude = highLatitude;
    [self.defaults setInteger:self.highLatitude forKey:HighLatitude];
    [AppDataManager sharedManager].highLatitude = self.highLatitude;
}

- (void)savePrayTimeCalMeth:(NSInteger)prayTimeCalMeth{
    self.prayTimeCalMeth = prayTimeCalMeth;
    [self.defaults setInteger:self.prayTimeCalMeth forKey:KeyPrayTimeCalMeth];
    [AppDataManager sharedManager].prayTimeCalMeth = self.prayTimeCalMeth;
}

- (void)saveReadingProgress:(BOOL)readingProgress{
//    _enableReadProgress = readingProgress;
//    [self.defaults setBool:readingProgress forKey:ReadingProgress];
//    [AppDataManager sharedManager].enableReadProgress = readingProgress;
}

- (void)saveQuranTransLan:(LanguageModel *)lan{
    self.tranLan = lan;
    [NSKeyedArchiver archiveRootObject:lan toFile:[NSString stringWithFormat:@"%@/tranLan",self.path]];
}

- (void)saveMp3Lan:(LanguageModel *)lan{
    self.mp3Lan = lan;
    [NSKeyedArchiver archiveRootObject:lan toFile:[NSString stringWithFormat:@"%@/mp3Lan",self.path]];
}

@end
